package com.yjps.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjps.common.message.Message;
import com.yjps.common.page.Pager;
import com.yjps.system.dao.ResumeDao;
import com.yjps.system.model.Resume;
import com.yjps.system.model.User;
import com.yjps.system.service.ResumeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 需求控制类
 *     |--后台接口
 *           |--/system/resume/list  后台管理分页显示简历列表
 *           |--/system/demand/accept  受理简历
 *           |--/system/demand/getDemand  获得指定id的需求json对象
 *           |--/system/resume/delete.do  删除简历
 *           |--/system/resume/getResume  后台获得指定id的简历对象
 *    |--前台接口
 *           |--/company/resume/edit.do  前台用户添加简历
 *           |--/company/resume/update.do  用户请求更新简历
 *           |--/company/resume/forwardEdit.do 进一步完善简历
 *           |--/company/resume/getResume.do  前台获得指定id的简历对象
 *
 * Created by jessy on 2015/8/24
 */
@Controller
public class ResumeController {


    private static final Logger log = LoggerFactory.getLogger(ResumeController.class);

    @Autowired
    ResumeService resumeService;

    /**
     * 后台管理分页显示简历列表
     *
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/system/resume/list", method = RequestMethod.GET)
    public String getList(Model model, HttpServletRequest request, HttpServletResponse response) {
        Pager<Resume> pager = resumeService.getResumeByPager(new Pager<Resume>(request, response));
        model.addAttribute(pager);
        return "system/resume/list";
    }

    /**
     * 前台用户添加简历 /company/resume/edit
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/company/resume/edit.do", method = RequestMethod.POST)
    public String edit(@RequestParam(value = "json", required = true) String json, HttpServletRequest request, HttpServletResponse response) {
        Message message = null;
        try {
            Resume resume = new Resume();
            JSONObject jsonObject = JSON.parseObject(json);
            resume.setName((String) jsonObject.get("name"));
            String sex = jsonObject.get("sex").toString();
            resume.setSex(Boolean.parseBoolean(sex));
            String age = jsonObject.get("age").toString();
            resume.setAge(Integer.parseInt(age));
            String job = jsonObject.get("job").toString();
            resume.setJob(job);
            String mobile = jsonObject.get("mobile").toString();
            resume.setMobile(mobile);
            String email = jsonObject.get("email").toString();
            resume.setEmail(email);
            String expression = jsonObject.get("expression").toString();
            resume.setExpression(expression);
            String honor = jsonObject.get("honor").toString();
            resume.setHonor(honor);
            String skill = jsonObject.get("skill").toString();
            resume.setSkill(skill);
//            User user = (User) request.getSession().getAttribute("loginUser");
            resume.setIsValid(1);
//            resume.setUserId(user.getId());
            resumeService.add(resume);
            log.debug("添加简历成功");
            message = new Message(true, "添加简历成功", resume.getId());
        } catch (Exception e) {
            System.out.println("exception = " + e.getClass());
            message = new Message(false, "添加简历失败");
        }
        return message.toJson();
    }

    /**
     * 更新简历
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/company/resume/update.do", method = RequestMethod.GET)
    public String update(@RequestParam(value = "json", required = true) String json, HttpServletResponse response) {
        Message message = null;
        try {
            JSONObject jsonObject = JSON.parseObject(json);
            String id = jsonObject.get("id").toString();
            System.out.println("json = " + json);
            Resume resume = resumeService.find(Integer.parseInt(id));
            resume.setName((String) jsonObject.get("name"));
            String sex = jsonObject.get("sex").toString();
            resume.setSex(Boolean.parseBoolean(sex));
            String age = jsonObject.get("age").toString();
            resume.setAge(Integer.parseInt(age));
            String job = jsonObject.get("job").toString();
            resume.setJob(job);
            String mobile = jsonObject.get("mobile").toString();
            resume.setMobile(mobile);
            String email = jsonObject.get("email").toString();
            resume.setEmail(email);
            String expression = jsonObject.get("expression").toString();
            resume.setExpression(expression);
            String honor = jsonObject.get("honor").toString();
            resume.setHonor(honor);
            String skill = jsonObject.get("skill").toString();
            resume.setSkill(skill);
            resumeService.update(resume);
            log.debug("更新 " + resume.getId() + "简历成功");
            message = new Message(true, "更新" + resume.getId() + " 简历成功",resume);
        } catch (Exception e) {
            message = new Message(false, "更新失败");
        }
        return message.toJson();
    }

    /**
     * 进一步完善简历
     * @param json 包含 id 简历id；job 期待岗位；expect_salar 期待薪资 三个参数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/company/resume/forwardEdit.do", method = RequestMethod.POST)
    public String forwardEdit(HttpServletResponse response,
                              @RequestParam(value = "json", required = true) String json) {
        Message message = null;/**/
        try {
            System.out.println("json = " + json);
            JSONObject jsonObject = JSON.parseObject(json);
            String expect_salar = jsonObject.get("expect_salar").toString();
            String job = jsonObject.get("job").toString();
            String id = jsonObject.get("id").toString();
            System.out.println("id >>>> " + id);
            Resume resume = resumeService.find(Integer.parseInt(id));
            System.out.println("resume >>>> " + resume);
            resume.setExpect_salar(Integer.parseInt(expect_salar));
            resume.setJob(job);
            resumeService.update(resume);
            message = new Message(true, "进一步完善简历成功");
        } catch (Exception e) {
            message = new Message(false, "完善失败");
        }
//        response.setHeader("Access-Control-Allow-Origin", "*");
        return message.toJson();
    }

    /**
     * 删除简历
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/system/resume/delete", method = RequestMethod.GET)
    public String delete(@RequestParam(value = "id", required = true) String id, Model model) {
        Message message = null;
        Resume r = resumeService.find(Integer.parseInt(id));
        if (r != null) {
            resumeService.delete(r);
            message = new Message(true, "delete successfully ");
            model.addAttribute("message", message);

        } else {
            log.debug("删除简历失败");
            message = new Message(false, "删除简历失败");
            model.addAttribute("message", message);
        }

        return "redirect:/system/resume/list";
    }

    /**
     * 前台获得指定id简历对象
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/company/resume/getResume.do", method = RequestMethod.GET)
    public String getResumeFront(@RequestParam(value = "id", required = true) String id) {
        Message message = null;
        try {
            Resume resume = resumeService.find(Integer.parseInt(id));
            message = new Message(true,"获得简历对象",resume);
        }catch (Exception e){
            message = new Message(false,"获取失败");
        }
        return message.toJson();
    }


    /**
     * 后台获得指定id简历对象
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/system/resume/getResume", method = RequestMethod.GET)
    public String getResume(@RequestParam(value = "id", required = true) String id) {
            Resume resume = resumeService.find(Integer.parseInt(id));
        return resume.toJson();
    }
}
